package com.example.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/8/30.
 */

public class CanvasView extends View {
    public CanvasView(Context context) {
        super(context);
    }

    public CanvasView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        //画线
        canvas.drawLine(0, 0, 100, 100, paint);
        //文本
        paint.setTextSize(20);
        paint.setColor(Color.RED);
        canvas.drawText("大师哥", 200, 200, paint);

        //画点
        paint.setStrokeWidth(5);
        canvas.drawPoint(150, 150, paint);
        //弧度
        paint.setStyle(Paint.Style.STROKE);
        RectF rectF = new RectF(100, 100, 200, 200);
        canvas.drawArc(rectF, 30, 60, true, paint);
        //画图片   Bitmap 位图
        //做一个Bitmap对象出来
        //1 将项目中已有的图片转为bitmap
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        //2 从零开始,完全自己创建
        Bitmap createdBmp = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                createdBmp.setPixel(i, j, Color.RED);
            }
        }
        canvas.drawBitmap(createdBmp, 150, 150, paint);
    }
}
